<template>
  <div class="table-main">
    <el-dialog
      :close-on-click-modal="false"
      @close="handMyClose"
      :append-to-body="true"
      title="商品轮播录入"
      :visible.sync="dialogVisible1"
      width="60%"
    >
      <div style="margin-top:50px;">
        <el-form ref="form" :model="form" label-width="120px" :rules="rules">
          <el-row :gutter="20" justify="center" type="flex">
            <el-col :span="16">
              <el-form-item label="轮播图名字:" prop="imageTitle">
                <el-input
                  :disabled="isShow == 3"
                  v-model="form.imageTitle"
                  placeholder="请输入"
                ></el-input>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row :gutter="20" justify="center" type="flex">
            <el-col :span="16">
              <el-form-item label="点击跳转地址:" prop="redictUrl">
                <el-input
                  :disabled="isShow == 3"
                  v-model="form.redictUrl"
                  placeholder="请输入"
                >
                  <!-- <template v-if="isShow==0" slot="prepend">Http://</template> -->
                </el-input>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row :gutter="20" justify="center" type="flex">
            <el-col :span="16">
              <el-form-item label="上传图片:" prop="resPath">
                <el-upload
                  :on-change="uploadChange"
                  v-loading="loading2"
                  v-if="isShow != 3"
                  :on-error="onError"
                  :headers="headers"
                  accept=".jpg,.png,.gif.svg,.bmp,.jpeg"
                  class="avatar-uploader"
                  :action="$store.state.app.newUrl + '/admin/wxUser/uoloadfile'"
                  :show-file-list="false"
                  :on-success="handSuccess"
                  :before-upload="beforeAvatarUpload"
                >
                  <img
                    v-if="form.resPath != undefined"
                    :src="form.resPath"
                    class="avatar"
                  />
                  <i v-else class="el-icon-plus avatar-uploader-icon"></i>
                  <div slot="tip" class="el-upload__tip">
                    只能上传700*288大小的图片
                  </div>
                </el-upload>
                <img v-if="isShow == 3" :src="form.resPath" class="avatar" />
              </el-form-item>
            </el-col>
          </el-row>
        </el-form>
      </div>

      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible1 = false">取 消</el-button>
        <el-button
          type="primary"
          @click="handFromSub('form')"
          :loading="bntLoading"
          v-if="isShow != 3"
          >确 定</el-button
        >
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { imgSizi } from "@/utils/myUtils";
export default {
  name: "SidebarLogo",
  components: {},
  watch: {
    isState(newObj) {
      this.isShow = newObj;
    }
  },
  props: {
    isState: {
      type: Number,
      default: 0
    }
  },
  data() {
    return {
      bntLoading: false,
      loading2: false,
      rules: {
        redictUrl: [
          { required: true, message: "请输入跳转地址", trigger: "blur" },
          {
            validator(rule, value, callback) {
              var reg = /^(((ht|f)tps?):\/\/)?[\w-]+(\.[\w-]+)+([\w.,@?^=%&:/~+#-]*[\w@?^=%&/~+#-])?$/;
              if (reg.test(value)) {
                callback();
              } else {
                callback(new Error("请输入正确的网址！"));
              }
            },
            trigger: "blur"
          }
        ],
        imageTitle: [
          { required: true, message: "请输入轮播图名字", trigger: "blur" }
        ],
        resPath: [{ required: true, message: "请上传轮播图" }]
      },
      headers: {
        token: localStorage.getItem("mytoken") || ""
      },
      bannerType: [
        {
          label: "汽车轮播图",
          value: 2
        },
        {
          label: "商品轮播图",
          value: 1
        },
        {
          label: "首页弹窗广告",
          value: 3
        }
      ],
      form: {},
      shopStatusOptions: [],
      isShow: 0,
      imageUrl: "",
      dialogImageUrl: "",
      dialogVisible1: false,
      addressKeyword: "",
      activeName: "first",
      dialogVisible: false,
      tipShow: false
    };
  },
  async mounted() {
    let res = await this.$api.getGoodsListl();
    this.shopStatusOptions = res.data.data;
  },
  methods: {
    uploadChange() {},
    onError() {},
    handGetShop(id) {
      this.$api.getBannerEditResources(id).then(res => {
        this.form = res.data.data;
        console.log("查询轮播详情2", res);
      });
    },
    handSuccess(res, file) {
      var url = JSON.parse(res.message);
      console.log(url, "res");
      this.$set(this.form, "resPath", url.url);
      this.loading2 = false;
    },
    beforeAvatarUpload(file) {
      const isLt2M = file.size / 1024 / 1024 < 2;
      const isJPG = file.type === "image/jpeg";
      if (!isLt2M) {
        this.$message.error("上传图片大小不能超过 2MB!");
      }
      const isSize = imgSizi(file, 700, 288);
      return isSize;
    },
    handMyClose() {
      this.$refs.form.resetFields();
      this.$parent.closeFrom();
    },
    handFromSub(formName) {
      //1编辑
      if (this.isState == 1) {
        this.$refs[formName].validate(valid => {
          if (valid) {
            this.bntLoading = true;
            this.$api
              .getShopBannereDitResources(this.form)
              .then(res => {
                this.dialogVisible1 = false;
                this.$parent.init();
                console.log(res, "编辑", this.form);
                this.$message({
                  message: res.data.message,
                  type: "success"
                });
                this.bntLoading = false;
              })
              .finally(() => {
                this.bntLoading = false;
              });
          } else {
            console.log("error submit!!");
            this.$message({
              message: "请完成表单录入"
            });
            this.bntLoading = false;
            return false;
          }
        });
      } else if (this.isState == 0) {
        this.$refs[formName].validate(valid => {
          if (valid) {
            this.bntLoading = true;
            this.form.imagePurpose = 4;
            this.$api
              .getShopBannerAdd(this.form)
              .then(res => {
                this.dialogVisible1 = false;
                this.bntLoading = false;
                this.$parent.init();
                this.$refs.form.resetFields();
                console.log(res, "添加轮播,表单验证", this.form);
                this.$message({
                  message: res.data.message,
                  type: "success"
                });
              })
              .finally(() => {
                this.bntLoading = false;
              });
          } else {
            console.log("error submit!!");
            this.$message({
              message: "请完成表单录入"
            });
            return false;
          }
        });
      }
    },

    // handSuccess(response, file, fileList) {
    //   console.log(response, file, fileList, '成功回调')
    //   this.form.shopImg = response.message
    // },
    handleRemove(file, fileList) {
      console.log(file, fileList);
    },
    // handlePictureCardPreview(file) {
    //   this.dialogImageUrl = file.url
    //   this.dialogVisible2 = true
    // },
    handleClose() {
      this.dialogVisible = false;
    },
    handMapShow() {
      this.dialogVisible = true;
    },
    handleClick() {}
  }
};
</script>

<style lang="scss" scoped>
.map_btn {
  width: 82px;
  height: 22px;
  background: rgba(56, 173, 147, 1);
  border-radius: 4px;
  color: #fff;
  font-size: 14px;
  line-height: 22px;
  text-align: center;
  margin-top: 8px;
}
/deep/ .el-form-item__label {
  font-size: 14px;
  color: #000;
  font-weight: bold;
}
.form_box {
  width: 80%;
  margin: 0 auto;
}
.avatar-uploader /deep/ .el-upload {
  border: 1px dashed #38ad93;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #38ad93;
  width: 336px;
  height: 90px;
  line-height: 90px;
  text-align: center;
}
.avatar {
  width: 336px;
  height: 90px;
  display: block;
}
</style>
